package com.lysj.bhp.exchangerate.api;

import com.google.gson.Gson;
import com.lysj.bhp.api.base.ApiContent;
import com.lysj.bhp.exchangerate.json.bcex.BcexCnyResp;
import com.lysj.bhp.exchangerate.json.bcexv2.BcexRateResp;
import com.lysj.bhp.exchangerate.json.bcexv2.Data;
import com.lysj.bhp.util.http.HttpUtil;
import com.lysj.bhp.util.web.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author zk
 * @description
 * @create 2018-07-25 10:21
 **/
@Service
public class BcexService extends ApiContent {

    private static Logger logger = LoggerFactory.getLogger(BcexService.class);

    private static String BCEX_TIKER_URL = "https://www.bcex.top/Api_Order/ticker";

    private static String DOLLAR_TO_CNY_URL = "https://www.bcex.top/coins/getBtcCny?type=bcex";

    /**
     * 获取以太坊币与美元的汇率
     *
     * @return
     */
    public BigDecimal getEthereumToDollar() {

        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(BCEX_TIKER_URL, "symbol=eth2ckusd");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        BigDecimal dataLast = data.getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        logger.info("[bcex内部接口]以太币对应美元汇率:" + dataLast);
        rate = dataLast;
        return rate;
    }


    /**
     * 获取比特币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBitcoinToDollar() {

        BigDecimal rate = new BigDecimal("-1");

        String respJson = HttpUtil.sendGet(BCEX_TIKER_URL, "symbol=btc2ckusd");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal dataLast = data.getLast();
        if (dataLast == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        logger.info("[bcex内部接口]比特币对应美元汇率:" + dataLast);
        rate = dataLast;
        return rate;
    }


    /**
     * @author drj
     * @date 2018/07/31 20:45
     * @Description:算力币对美元的汇率
     */
    public BigDecimal getBhpcToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(BCEX_TIKER_URL, "symbol=bhpc2eth");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal bhpcToEthRate = data.getLast();
        if (bhpcToEthRate == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        rate = bhpcToEthRate.multiply(getEthereumToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        logger.info("[bcex内部接口]算力币对美元汇率:" + rate);

        return rate;
    }


    /**
     * @author drj
     * @date 2018/07/31 20:42
     * @Description:获取人民币对美元的汇率
     */
    public BigDecimal cnyToDollarRate() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(DOLLAR_TO_CNY_URL, "");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexCnyResp bcexCnyResp = new Gson().fromJson(respJson, BcexCnyResp.class);
        if (bcexCnyResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        String dollar2cny = bcexCnyResp.getUsd2cnyrate(); //获取美元对人民币的汇率
        if (ParamUtil.isBlack(dollar2cny)) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        rate = new BigDecimal("1").divide(new BigDecimal(dollar2cny), 6, BigDecimal.ROUND_DOWN);
        logger.info("[bcex内部接口]人民币对美元的汇率:"+rate);
        return rate;
    }

    /**
     * @author zwj
     * @date 2018-08-01 17:01
     * @Description:获取比特币砖石对美元的汇率
     */
    public BigDecimal getDiamoundToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(BCEX_TIKER_URL, "symbol=bcd2btc");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal diamoundToBtcRate = data.getLast();
        if (diamoundToBtcRate == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        rate = diamoundToBtcRate.multiply(getBitcoinToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        logger.info("[bcex内部接口]比特币砖石对美元的汇率:" + rate);

        return rate;
    }

    /**
     * @author zwj
     * @date 2018-08-02 09:48
     * @Description:获取布比币对美元的汇率
     */
    public BigDecimal getBuToDollar() {
        BigDecimal rate = new BigDecimal("-1");
        String respJson = HttpUtil.sendGet(BCEX_TIKER_URL, "symbol=bu2eth");
        if (ParamUtil.isBlack(respJson)) {
            return rate;
        }
        BcexRateResp bcexRateResp = new Gson().fromJson(respJson, BcexRateResp.class);
        if (bcexRateResp == null) {
            logger.error("Json格式化错误:" + respJson);
            return rate;
        }
        if (bcexRateResp.getCode() != 0) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        Data data = bcexRateResp.getData();
        if (data == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }

        BigDecimal buToEthRate = data.getLast();
        if (buToEthRate == null) {
            logger.error("接口数据错误:" + respJson);
            return rate;
        }
        rate = buToEthRate.multiply(getEthereumToDollar()).setScale(6, BigDecimal.ROUND_DOWN);
        logger.info("[bcex内部接口]布比币对美元的汇率:" + rate);

        return rate;
    }




}
